<template>
  <div class="login">
    <h1>Edu boss管理系统</h1>
    <el-form
      class="form"
      label-position="top"
      :model="form"
      ref="form"
      :rules="rules"
    >
      <h2>登录</h2>
      <el-form-item label="手机号" prop="phone">
        <el-input
          type="phone"
          v-model="form.phone"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          class="submitBtn"
          @click="submitForm"
          :loading="!!submitting"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
    <div class="footer">←回到用户端</div>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { Form } from 'element-ui'
import { login, LoginUserRequest as LoginUser } from '@/services/user'
export default Vue.extend({
  name: 'LoginIndex',
  data (): { submitting: Promise<void> | null; form: unknown } {
    return {
      /**
       * @type {Promise} 如何声明类型
       */
      submitting: null,
      form: {
        phone: '15510792995',
        password: '111111'
      }
    }
  },
  computed: {
    rules () {
      return {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '手机号无效', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'change' }
        ]
      }
    }
  },
  created () {
    const _submitForm = this.submitForm.bind(this)
    this.submitForm = async () => {
      if (!this.submitting) {
        this.submitting = _submitForm()
        this.submitting.finally(() => (this.submitting = null))
      }
      return this.submitting
    }
  },
  methods: {
    async submitForm () {
      try {
        // 表单验证
        await (this.$refs.form as Form).validate()
      } catch {
        return
      }
      try {
        // 请求
        const { data } = await login(this.form as LoginUser)

        if (data.state !== 1) {
          this.$message.error(data.message)
          return
        }
        this.$store.commit('setUser', JSON.parse(data.content))
        this.$message.success('登录成功')
        // 响应成功
        this.$router.push(this.$route.query.redirect as string || '/')
      } catch (error) {
        console.log('err', error)
        this.$message.error('验证失败:' + error.message)
      }
    }
  }
})
</script>
<style lang="scss" scoped>
.login {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .form {
    box-sizing: border-box;
    width: 300px;
    padding: 0 2em;
    background-color: #fff;
    border-radius: 5px;
    .submitBtn {
      width: 100%;
    }
  }
  .footer {
    box-sizing: border-box;
    margin-top: 2em;
    width: 300px;
    padding: 0.5em 0;
    border: 1px solid #999;
    text-align: center;
  }
}
</style>
